import random
import time
import requests
import urllib3
import datetime
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
headers = {
    'User-Agent':'PPKS/2.8.9 (iPad; iOS 15.3.1; Scale/2.00)'
}
url = 'https://j1.pupuapi.com/client/product/storeproduct/detail/7c1208da-907a-4391-9901-35a60096a3f9/fc61c0a6-83ac-425c-98b7-0422399f99c6'

# 请求接口
def getPupu():
    #生成一个随机秒数，每隔一段秒数进行刷新
    randoms = random.randint(3,6)
    result = requests.get(url,headers = headers,verify=False).json()
    time.sleep(randoms)
    return result

result = getPupu()
# 清洗数据
def extract_data(result):
    spec = str(result['data']['spec'])
    name = str(result['data']['name'])
    price = str(result['data']['price'] / 100)
    market_price = str(result['data']['market_price'] / 100)
    sub_title = str(result['data']['sub_title'])
    return spec, name, price, market_price, sub_title
spec, name, price, market_price, sub_title = extract_data(result)

def getTime():
    nowTime = str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) #添加时间
    return nowTime
print('-------------------------商品：'+name+'---------------------------------------')
print("规格："+spec)
print("价格："+price)
print("原价/折扣价："+market_price+"/"+price)
print("详细内容："+sub_title)
print('-------------------------商品：'+name+'的价格波动--------------------------------')


if __name__ == '__main__':
    while(1):
        result = getPupu()
        price = extract_data(result)
        print("当前时间为"+getTime()+", 价格为"+price[2])
